﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FtpWorkerRole.FtpServer;

namespace FtpWorkerRole.FtpCommand
{
    public class CdupCommand : CommandBase
    {
        public CdupCommand(FtpSocketHandler handler)
            : base(handler)
        {
            commandName = "CDUP";
        }

        public override void execute(string value)
        {
            if (!handler.Login)
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(530, "Please Login!"));
                return;
            }
            var path = handler.currentDirectory;
            if (path == @"\")
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(550, "It is already a root directory."));
                return;
            }
            int idx = path.LastIndexOf(@"\");
            if (idx > -1)
            {
                handler.currentDirectory = path.Remove(idx + (idx == 0 ? 1 : 0));
            }

            Utils.sendMessage(handler.getSocket(), formatMessage(250, "Directory successfully changed."));
        }
    }
}
